<?php

/**
 * trip actions.
 *
 * @package    maximous
 * @subpackage trip
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class tripActions extends MaxFrontendActions {

    public function preExecute() {
        parent::preExecute();
        if ($this->maxUser) {
            $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::Trip);
        }
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        $query = TripTable::getInstance()->getAllTripsQuery();
        $page = $request->getParameter('page', 1);
        $this->pager = $this->createPager('Job', $query, $page, sfConfig::get('app_record_per_page'));
    }

    public function executeView(sfWebRequest $request) {

        $tripTitle = $request->getParameter('rtitle');
        $trip = TripTable::getInstance()->findOneByTitle($tripTitle);
        $this->forward404Unless($trip);

        $watchedTrip = $this->getUser()->getAttribute('watched_trip', array());
        if (!in_array($trip->getId(), $watchedTrip)) {
            $trip->increamentVisits();
            $trip->save();
            $watchedTrip[] = $trip->getId();
            $this->getUser()->setAttribute('watched_trip', $watchedTrip);
        }
        $this->trip = $trip;
    }

}
